using System;

namespace ActivUFRJ.Sistema
{
    public class DBAviso : Connection.MySQL
    {
        public DBAviso() : base(ref Connection.DBConnection.myConnection)
        {

        }

        private void FillAviso(Aviso noticia)
        {
            noticia.ID = Convert.ToInt32(this.TableGetValue("SIA_ID"));
            noticia.Nome = Convert.ToString(this.TableGetValue("SIA_NOME"));
            noticia.Descricao = Convert.ToString(this.TableGetValue("SIA_Descricao"));
            noticia.DataRegistro = Convert.ToDateTime(this.TableGetValue("SIA_DATA_REGISTRO"));
        }

        internal Aviso[] GetListaAvisos()
        {
            try
            {
                string SQL = "SELECT * from SISTEMA_AVISOS ORDER BY SIA_DATA_REGISTRO DESC";

                this.SetSQL(SQL);
                int i = 0;
                Aviso[] listaAviso = new Aviso[this.GetRowCount()];

                while (i < this.GetRowCount())
                {
                    Aviso novaNoticia = new Aviso();
                    this.FillAviso(novaNoticia);
                    listaAviso[i] = novaNoticia;
                    i++;
                    this.TableMoveNext();
                }

                return listaAviso;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        internal System.Data.DataTable GetListaAvisosDT()
        {
            try
            {
                // busca o 'login' do administrador do sistema.
                string SQL = "SELECT SIA_ID, SIA_NOME, SIA_DESCRICAO, SIA_DATA_REGISTRO, USR_Login " +
                                "from SISTEMA_AVISOS " +
                                "inner join usuarios on 1 = usuarios.usr_ID  " +
                                "ORDER BY SIA_DATA_REGISTRO DESC"; 

                this.SetSQL(SQL);
                return this.ReturnTable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}
